var showingPublic = false;
var publicTags;

/* Discussion Page Stuff */
function showPublicTags(objectid) {
	if (showingPublic) {
		$('showpublictags').innerHTML = 'Show All';
		$('othertags').parentNode.removeChild($('othertags'));
		Element.removeClassName('populartags', 'commentinfo');
		showingPublic = false;
	} else {
		$('showpublictags').innerHTML = 'Hide';
		
		showingPublic = true;
		
		var othertags = document.createElement('div');
		othertags.id = 'othertags';
		othertags.innerHTML = '<em>Loading...</em>';
		$('populartags').parentNode.insertBefore(othertags, $('populartags').nextSibling);
		
		Element.addClassName('populartags', 'commentinfo');
		$('populartags').style.marginTop = '10px';
		
		if (publicTags) {
			showPublicTagsContent();
		} else {
			callWebService('Object', 'othertags', 'object='+objectid, function (request) { if (request.responseText) {publicTags = eval("("+request.responseText+")"); } showPublicTagsContent(); });
		}
	}
	
	return false;
}

function tagSearch() {
	var term = $('searchothertags').value.toLowerCase();
	for (i=0; i<publicTags.length; i++) {
		var found = false;
		if (term) {
			if (publicTags[i].name.toLowerCase().match(term)) {
				found = true;
			} else {
				for (var j in publicTags[i].tags) {
					if (typeof(publicTags[i].tags[j]) == "string") {
						if (publicTags[i].tags[j].match(term)) {
							found = true;
						}
					}
				}
			}
		} else {
			found = true;
		}

		if (found) {
			Element.show('othertags'+publicTags[i].name);
		} else {
			Element.hide('othertags'+publicTags[i].name);
		}
	}
}

function showPublicTagsContent() {
	if (publicTags) {
		var othertags = '';
		var tags = {};
		
		for (i=0; i<publicTags.length; i++) {
			othertags += '<tr id="othertags'+publicTags[i].name+'"><td class="member">'+publicTags[i].name+':</td><td class="tags">';
			for (var j in publicTags[i].tags) {
				if (typeof(publicTags[i].tags[j]) == "string") {
					othertags += '<a href="'+tagURL+'~'+publicTags[i].name+'/'+publicTags[i].tags[j]+'">'+publicTags[i].tags[j]+'</a> ';
				}
			}
			othertags += '</td></tr>';
		}
	
		$('othertags').innerHTML = '<p style="margin-top: 0;">Search: <input type="text" id="searchothertags" onkeyup="javascript:tagSearch();" /></p><table><tr><th class="member">Member</th><th class="tags">Tags</th></tr>'+othertags+'</table><p><a href="#" onclick="javascript:return showPublicTags();">Close User Tags</a></p>';
	} else {
		$('othertags').innerHTML = '<p><em>Nobody has tagged this discussion yet.</em></p><p><a href="#" onclick="javascript:return showPublicTags();">Close User Tags</a></p>';
	}
}

/* Quick Editing of Tags */
function quickTag() {
	if ($('tageditor')) {
		Element.hide('mytags');
		var tageditor = $('tageditor').innerHTML;
		var tagvalue = $('tags').value;
		$('tageditor').innerHTML = '<p class="fieldlabel">Discussion Tags:</p><p class="field">The quick tag editor is currently open. <a href="#" onclick="javascript:cancelQuickTag(true, true);">Close the tag editor</a> to tag here.</p>';
		$('tageditor').className = '';
		
		var quicktagextra = document.createElement('div');
		quicktagextra.id = 'quicktagextra';
		quicktagextra.innerHTML = '<p class="commentinfo"><strong>Edit My Tags</strong></p><div id="quicktageditor"><div id="quicktagform">'+tageditor+'</div><p><input type="button" value="Save My Tags" onclick="saveQuickTag();" /> <input type="button" value="Cancel" onclick="cancelQuickTag();" /></p></div>';
		$('mytags').parentNode.insertBefore(quicktagextra, $('mytags').nextSibling)

		$('tags').value = tagvalue;
		initTags();
		updateHighlight();
	}
	
	$('tags').focus();
}

function saveQuickTag() {
	callWebService('Object', 'savemytags', 'object='+tagObjectId+'&tags='+$('tags').value, function (request) {});
	
	var tags = $('tags').value.split(" ");
	var taglinks = new Array();
	for (i=0; i<tags.length; i++) {
		if (tags[i]) {
			tags[i] = tags[i].toLowerCase();
			taglinks.push('<a href="'+userTagURL+tags[i]+'">'+tags[i]+'</a>');
		}
	}
	
	$('quicktag').innerHTML = taglinks.join(" ")+' ( <a href="#" onclick="javascript:return quickTag();">Edit Tags</a> )';
	
	if ($('edittaglist')) {
		var tagcode = $('tags').value.toLowerCase();
		var escapeH = document.createElement('div');
	    escapeH.appendChild(document.createTextNode(tagcode));
	    $('edittaglist').innerHTML = escapeH.innerHTML;
	}
	
	cancelQuickTag(true, false);
}

function cancelQuickTag(preserve, focus) {
	Element.show('mytags');
	var tageditor = $('quicktagform').innerHTML;
	
	if (preserve) {
		var tags = $('tags').value;
	}
	
	$('tageditor').innerHTML = tageditor;
	
	$('quicktagextra').parentNode.removeChild($('quicktagextra'));
	
	if (preserve) {
		$('tags').value = tags;
	}
	
	if (focus) {
		$('tags').focus();
	}
	
	initTags();
	findTags();
}